<?php

namespace app\v1\common\controller;

use app\common\model\AutionLogChange;
use app\common\model\AutionLogChangeChange;
use app\common\model\Crontab;
use app\common\model\Goods;
use app\common\model\Order;
use app\common\model\Slide;
use app\common\model\User;
use think\Config;
use think\Controller;
use think\Db;
use think\Log;
use think\Request;


class GoodsController extends Controller
{
    public function __construct()
    {
        Config::load(APP_PATH . 'v1/common/config.php');
    }

    /**
     * 商品列表
     */
    public function goodsList(Request $request)
    {
        $limit = $request->param('limit') ?: 10;
        $page  = $request->param('page') ?: 1;
        $data  = Goods::where(['is_on_sale' => 1])->field([
            'goods_id',
            'goods_name',
            'shop_price',
            'original_img',
            'goods_content',
            'start_price',
            'raise_price',
            'sales_end_time',
        ])->paginate($limit,
            false, ['page' => $page]);
        //循环找出当前商品的拍到哪个价格了
        foreach ($data as $item => $value) {
            $aution_log                   = AutionLogChange::where(['goods_id' => $value['goods_id']])->field(['current_price'])->order(['current_price' => 'desc'])->find();
            $data[$item]['current_price'] = empty($aution_log['current_price']) ? $value['start_price'] : $aution_log['current_price'];
        }

        return json(['code' => 200, 'info' => '已获取到数据', 'data' => $data]);
    }

    /**
     * @param Request $request
     * @return \think\response\Json
     * 商品详细
     */
    public function goodsInfo(Request $request)
    {
        $goods_id   = $request->param('goods_id');
        $aution_log = AutionLogChange::where(['goods_id' => $goods_id])->field(['current_price'])->order(['current_price' => 'desc'])->find();

        $data = Goods::where(['goods_id' => $goods_id])->where(['is_on_sale' => 1])->field([
            'goods_id',
            'goods_name',
            'shop_price',
            'goods_content',
            'start_price',
            'original_img',
            'detail_img',
            'raise_price',
            'sales_end_time',
        ])->find();
        if (empty($data)) {
            return json(['code' => 500, 'info' => '商品不存在或者已下架']);
        }
//        $data['detail_img']    = explode(',', $data['detail_img']);
        $data['original_img']  = explode(',', $data['original_img']);
        $data['current_price'] = empty($aution_log['current_price']) ? $data['start_price'] : $aution_log['current_price'];

        $content               = htmlspecialchars_decode($data['goods_content']);
        $domain                = $request->domain();
        $str                   = $domain . "/public";
        $content_new           = str_replace('src="/public', 'src="' . $str, $content);
        $data['goods_content'] = ($content_new);

        return json(['code' => 200, 'info' => '已获取到数据', 'data' => $data]);

    }


    /**
     * @param Request $request
     * @return \think\response\Json
     * 定时任务
     */
    public function clockPublish(Request $request)
    {

        //最后两分钟,直接拉到最高底价
//        $time       = time();
//        $start_time = $time - 120;
//        $end_time   = $time - 60;
//
//        $wheres_time_other['end_time'] = ['between', "$start_time,$end_time"];
//        $crontab_data_other            = Crontab::where($wheres_time_other)->field([
//            'id',
//            'goods_id'
//        ])->select();
//        dd($crontab_data_other);
//        if (!empty($crontab_data_other)) {
//            foreach ($crontab_data_other as $item => $value) {
//                //先查这个商品有没有被竞拍过
//                $aution_log = AutionLogChange::where(['goods_id' => $value['goods_id']])->field(['current_price'])->order(['current_price' => 'desc'])->find();
//
//                $goods_info = Goods::where(['goods_id' => $value['goods_id']])->find();
//                //最后两分钟,没人出到系统设定的价格的话,直接拉到最高价格
//                if ($goods_info['shop_price'] > $aution_log['current_price']) {
//                    $data_user = [
//                        'current_price' => $goods_info['shop_price'],
//                        'user_id' => 0,
//                        'user_name' => 'system',
//                        'address' => 'address',
//                        'goods_id' => $value['goods_id'],
//                        'add_price' => $goods_info['shop_price'] - $aution_log['current_price'],
//                        'use_coin' => $goods_info['shop_price'] - $aution_log['current_price'],
//                        'shop_price' => $goods_info['shop_price'],
//                        'total_use_coin' => $goods_info['shop_price'] - $aution_log['current_price'],
//                        //总共花费了多少币
//                        'total_add_money' => $goods_info['shop_price'] - $aution_log['current_price']
//                        //总共花费了多少钱
//                    ];
//                    AutionLogChange::create($data_user);
//                }
//            }
//        }


        $crontab_data = Crontab::where('end_time', '<=', time())->field([
            'id',
            'goods_id'
        ])->select();
        if (!empty($crontab_data)) {
            Db::transaction(function () use ($crontab_data) {
                foreach ($crontab_data as $item => $value) {
                    Goods::where(['goods_id' => $value['goods_id']])->update(['is_on_sale' => 0]);
                    Crontab::destroy($value['id']);
                }
            });
        }

        return json(['date' => 'success']);
    }

    /**
     * @param Request $request
     * 获取竞拍价格
     */
    public function getGoodsAutionPrice(Request $request)
    {
        $goods_id = $request->param('goods_id');

        $goods_info = Goods::where(['goods_id' => $goods_id])->field([
            'goods_id',
            'goods_name',
            'shop_price',
            'goods_content',
            'start_price',
            'raise_price',
            'sales_end_time',
        ])->find();

        if (empty($goods_info)) {
            return json(['code' => 500, 'info' => '商品不存在']);
        }
        $data   = AutionLogChangeChange::where(['goods_id' => $goods_id])->field([
            'goods_id',
            'current_price'
        ])->order(['current_price' => 'desc'])->find();
        $result = empty($data['current_price']) ? $goods_info['start_price'] : $data['current_price'];
        return json(['code' => 200, 'info' => '已获取到数据', 'data' => ['current_price' => $result]]);
    }


    /**
     * @param Request $request
     * 获取banner
     */
    public function bannerList(Request $request)
    {
        $where   = [];
        $keyword = $request->param('keyword') ?: "";
        $page    = $request->param('page') ?: 1;
        $limit   = $request->param('limit') ?: 10;

        if ($keyword) {
            $where['name'] = ['like', "%{$keyword}%"];
        }

        $slide_list = Slide::where($where)->order(['sort' => 'DESC'])->paginate($limit, false, ['page' => $page]);

        return json(['code' => 200, 'info' => 'success', 'data' => $slide_list]);
    }


}
